import { Controller, Get, Query, UseGuards } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth } from '@nestjs/swagger';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { RolesGuard } from '../auth/roles.guard';
import { Roles } from '../auth/roles.decorator';
import { StaticService } from './static.service';
import { SalesStatisticsDto, TopProductsDto } from './static.dto';

@ApiTags('统计')
@Controller('statistics')
@UseGuards(JwtAuthGuard, RolesGuard)
@ApiBearerAuth()
export class StaticController {
  constructor(private readonly statisticsService: StaticService) {}

  @Get('overview')
//   @Roles('admin')
  @ApiOperation({ summary: '获取概览数据' })
  @ApiResponse({ status: 200, description: '获取成功' })
  @ApiResponse({ status: 401, description: '未授权' })
  @ApiResponse({ status: 403, description: '权限不足' })
  @ApiResponse({ status: 500, description: '服务器错误' })
  async getOverview() {
    return this.statisticsService.getOverview();
  }

  @Get('sales')
  @Roles('admin')
  @ApiOperation({ summary: '获取销售统计数据' })
  @ApiResponse({ status: 200, description: '获取成功' })
  @ApiResponse({ status: 400, description: '参数错误' })
  @ApiResponse({ status: 401, description: '未授权' })
  @ApiResponse({ status: 403, description: '权限不足' })
  @ApiResponse({ status: 500, description: '服务器错误' })
  async getSalesStatistics(@Query() query: SalesStatisticsDto) {
    return this.statisticsService.getSalesStatistics(
      new Date(query.startDate),
      new Date(query.endDate),
    );
  }

  @Get('categories')
  @Roles('admin')
  @ApiOperation({ summary: '获取分类统计数据' })
  @ApiResponse({ status: 200, description: '获取成功' })
  @ApiResponse({ status: 401, description: '未授权' })
  @ApiResponse({ status: 403, description: '权限不足' })
  @ApiResponse({ status: 500, description: '服务器错误' })
  async getCategoryStatistics() {
    return this.statisticsService.getCategoryStatistics();
  }

  @Get('top-products')
  @Roles('admin')
  @ApiOperation({ summary: '获取热门商品统计数据' })
  @ApiResponse({ status: 200, description: '获取成功' })
  @ApiResponse({ status: 400, description: '参数错误' })
  @ApiResponse({ status: 401, description: '未授权' })
  @ApiResponse({ status: 403, description: '权限不足' })
  @ApiResponse({ status: 500, description: '服务器错误' })
  async getTopProducts(@Query() query: TopProductsDto) {
    return this.statisticsService.getTopProducts(query.limit);
  }

  @Get('users')
  @Roles('admin')
  @ApiOperation({ summary: '获取用户统计数据' })
  @ApiResponse({ status: 200, description: '获取成功' })
  @ApiResponse({ status: 401, description: '未授权' })
  @ApiResponse({ status: 403, description: '权限不足' })
  @ApiResponse({ status: 500, description: '服务器错误' })
  async getUserStatistics() {
    return this.statisticsService.getUserStatistics();
  }

  @Get('order-status')
  @Roles('admin')
  @ApiOperation({ summary: '获取订单状态统计数据' })
  @ApiResponse({ status: 200, description: '获取成功' })
  @ApiResponse({ status: 401, description: '未授权' })
  @ApiResponse({ status: 403, description: '权限不足' })
  @ApiResponse({ status: 500, description: '服务器错误' })
  async getOrderStatusStatistics() {
    return this.statisticsService.getOrderStatusStatistics();
  }
}
